﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SharedGenomics.Core;
using SharedGenomics.Workbench.PresentationModel;
using SharedGenomics.Workbench.Models;

namespace SharedGenomics.Workbench.Views.WindowsForms
{
    public partial class WatchListView : View, IWatchListView
    {
        private WatchListModel _model;

        public WatchListView()
        {
            InitializeComponent();
        }

        private void btOpenWatchList_Click(object sender, EventArgs e)
        {
            this.OpenWatchListClicked.Raise(this);
        }

        public WatchListItem SelectedItem
        {
            get { return this.lstWatchList.SelectedItem as WatchListItem; }
        }

        public WatchListModel Model
        {
            private get { return this._model; }
            set
            {
                value.ThrowIfNull("Model");

                this._model = value;
                this._model.ListChanged += new EventHandler(this.Model_ListChanged);
            }
        }

        private void Model_ListChanged(object sender, EventArgs e)
        {
            this.lstWatchList.Items.Clear();
            this.lstWatchList.Items.AddRange(this.Model.Items.ToArray());
        }

        public event EventHandler OpenWatchListClicked;
        public event EventHandler SelectedItemChanged;

        private void lstWatchList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SelectedItemChanged.Raise(this);
        }

        private void lstWatchList_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0)
                return;

            WatchListItem item = (WatchListItem)this.lstWatchList.Items[e.Index];
            Brush textBrush = item.IsFlagged ? Brushes.Red : Brushes.Black;

            e.DrawBackground();
            e.DrawFocusRectangle();
            e.Graphics.DrawString(item.Name, e.Font, textBrush, e.Bounds);
        }
    }
}
